<template>
  <div class="app-container">
    <!-- <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="省份" prop="province">
          <el-select
            v-model="queryParams.province"
            placeholder="全部"
            clearable
            class="!w-[100px]"
            filterable
          >
            <el-option
              v-for="item in provinceCode"
              :value="item.name"
              :label="item.name"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div> -->

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="codeList"
        highlight-current-row
        border
        stripe
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
          :index="indexMethod"
        />
        <el-table-column label="时间" prop="time" min-width="100" />
        <el-table-column
          label="计费成功数"
          prop="success"
          width="120"
          sortable
        />
        <el-table-column
          label="计费请求数"
          prop="request"
          width="120"
          sortable
        />
        <el-table-column label="计费用户数" prop="user" width="120" sortable />
        <el-table-column
          label="请求转化率"
          prop="requestConversionRate"
          width="120"
          sortable
        >
          <template #default="scope">
            {{ convertToRate(scope.row.requestConversionRate) }}%
          </template>
        </el-table-column>
        <el-table-column
          label="用户转化率"
          prop="userConversionRate"
          width="120"
          sortable
        >
          <template #default="scope">
            {{ convertToRate(scope.row.userConversionRate) }}%
          </template>
        </el-table-column>
        <el-table-column
          label="升级数量"
          prop="validate"
          width="110"
          sortable
        />
        <el-table-column
          label="成功升级比例"
          prop="successValidateProportion"
          width="150"
          sortable
        />
      </el-table>

      <!-- <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.size"
        @pagination="handleQuery"
      /> -->
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { provinceCode } from "@/utils/constant";
import { getTimeStatisticsPage } from "@/api/timeStatistics";
import {
  TimeStatisticsQuery,
  TimeStatisticsPageVO,
} from "@/api/timeStatistics/types";
import { convertToRate } from "@/utils/index";

defineOptions({
  name: "Role",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
]; // '12:00:00', '08:00:00'
const indexMethod = (index: number) => {
  return index + 1;
};
const searchTime = ref(null);
const queryParams = reactive<TimeStatisticsQuery>({
  page: 1,
  size: 10,
});

const codeList = ref<TimeStatisticsPageVO[]>();

function initSearchTime() {
  const start = new Date(
    new Date(
      `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 00:00:00`
    ).getTime() -
      3600 * 1000 * 24 * 31
  );
  const end = new Date(
    `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 23:59:59`
  );
  searchTime.value = [start, end];
  console.log("🚀 ~ initSearchTime ~ searchTime:", searchTime);
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  if (queryParams.province === "全国") {
    queryParams.province = "";
  }
  getTimeStatisticsPage(queryParams)
    .then((data) => {
      console.log("🚀 ~ .then ~ data:", data);
      codeList.value = data;
      total.value = data.length;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  initSearchTime();
  handleQuery();
}

onMounted(() => {
  initSearchTime();
  handleQuery();
});
</script>
